//获取应用实例
const app = getApp()
var that
Page({
  data:{
    //text:"这是一个界面"
    actionSheetHidden_pic:true,   //下方是否显示选择头像拍照或相册的模块
    actionSheetHidden_sex:true,     //性别选择栏是否隐藏
    nickname: "用户名",
    sex: "男",
    head_picurl:"",       //个人头像路径
    address:"请填写地址",
    phone: "暂无",
    rr_msg:'',   //个人在微信商城的信息
  },
  onLoad:function(){
    that=this;
   
  },
  onReady:function(){
    //页面渲染完成
  },
  onShow:function(){
    that.get_p_msg()
    that.get_person_f_rr();//获取个人在人人商城的信息
  },
  onHide:function(){
    //页面隐藏
  },
  onUnload:function(){
    //页面关闭
  },
  choose_head_pic:function(){//点击头像时，下方选择显示
    this.setData({ actionSheetHidden_pic:false})
  },
  ActionSheet_select:function()//下方选项取消后即隐藏
  {
    this.setData({ actionSheetHidden_pic: true, actionSheetHidden_sex: true})
  },
  itemclick:function(e){//点击选项时触发
    var that=this;
    if (e.currentTarget.id==1)  //相机
    {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera'],  // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          console.log(res.tempFilePaths[0])
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: app.rootDocment + 'upload.html',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'sessionid': wx.getStorageSync('3rd_session'),
              'openid': app.data.openid,
              'type': "avter"
            },
            success: function (res) {
              // console.log(res);//设置头像
              var j = JSON.parse(res.data);
              app.update_person_msg("custom_photo", j.mess);
              var userInfo = wx.getStorageSync('userInfo');
              userInfo.custom_photo = j.mess;
              console.info('更新用户数据:'+userInfo);
              wx.setStorageSync('userInfo',userInfo);
              that.setData({ head_picurl: userInfo.custom_photo });
              // console.log(app.globalData.userInfo.custom_photo);
            }
          })
        }
      })
    }
    if (e.currentTarget.id == 2) {  //相册
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          console.log(res.tempFilePaths)
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: app.rootDocment + 'upload.html',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'sessionid': wx.getStorageSync('3rd_session'),
              'openid': app.data.openid,
              'type': "avter"
            },
            success: function (res) {
              // console.log(res);//设置头像
              var j= JSON.parse(res.data);

              app.update_person_msg("custom_photo", j.mess);
              var userInfo = wx.getStorageSync('userInfo');
              userInfo.custom_photo = j.mess;
              console.info('更新用户数据:' + userInfo);
              wx.setStorageSync('userInfo', userInfo);
              that.setData({ head_picurl: userInfo.custom_photo });
              // console.log(app.globalData.userInfo.custom_photo);
            }
          })
        }
      })
    }
    this.setData({ actionSheetHidden_pic: true })
  },
  change_sex:function(){ //点击性别时，性别栏不在隐藏
    this.setData({ actionSheetHidden_sex: false })
  },
  itemclick_sex: function (e) {//点击选项时触发
      var that = this;
      if (e.currentTarget.id == 1)  //男
      {
        that.setData({ sex: "男" });
        app.update_person_msg("sex", "1");
        app.globalData.userInfo.sex="1";
      }
      if (e.currentTarget.id == 2) {//女
        that.setData({ sex: "女" });
        app.update_person_msg("sex", "2");
        app.globalData.userInfo.sex = "2";
      }  
      this.setData({ actionSheetHidden_sex: true })
  },
  change_name:function(){  //点击姓名跳转到修改姓名界面
    // console.log(this.data.nickname);
    wx.navigateTo({
      url: './name?name=' + this.data.nickname
    });
  },
  change_address: function () {//点击地址栏跳转修改地址界面
    wx.navigateTo({
      url: './address?address=' + this.data.address
    });
  },
  change_phone: function () {//点击电话栏跳转修改电话界面

    if (this.data.phone =='暂无'){
      wx.navigateTo({url: './phone'});
    }else{
          wx.navigateTo({url: './phone_change?phone=' + this.data.phone });
    }
    
  },
  get_p_msg: function () {  //获取个人所有信息
    app.req("get_p.html", { "openid": app.data.openid }, function (res) {
      if (res.status == "success") {
        app.globalData.userInfo = res.mess;
        that.set_mse()
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_p_msg()
          }
        })
      }
    })
  },
  get_person_f_rr: function () {
    var openid = wx.getStorageSync('openid');
    app.req("get_person_f_rr.html", { "openid": openid }, function (res) {
      if (res.status == "success") {
        app.data.person_msg_rr = res.mess
        that.setData({ rr_msg: app.data.person_msg_rr })
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_person_f_rr()
          }
        })
      }
    })
  },
  set_mse:function(){
    var res = app.globalData.userInfo;
    if (res.custom_nickname == "") {   //设置用户名
      that.setData({ nickname: res.nickname });
    } else { that.setData({ nickname: res.custom_nickname }); }
    if (res.custom_photo == "") { //设置用户头像
      that.setData({ head_picurl: res.photo });
    } else { that.setData({ head_picurl: res.custom_photo }); }
    if (res.sex == 2) {     //设置用户性别
      that.setData({ sex: '女' });
    }
    if (res.address != "") { //设置用户地址
      var adr = app.globalData.userInfo.address;
      that.setData({ address: adr.split("-")[0] + "-" + adr.split("-")[1] + "-" + adr.split("-")[2] });
    }
    if (res.phone != "") { //设置用户电话
      that.setData({ phone: res.phone });
    }
  }
})